package engine;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Time;
import java.util.*;

import com.managment.data.Media;
import com.managment.data.Task;


public class Utils { 
	private static char [] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','r','s','t','u','w','x','y','z'};
    
	public static void printTask(Task task){
		System.out.println("Medium: "+task.getMedia().toString());
		System.out.println("Operacja: "+task.getState());
		if(task.getEventTime() != null)
			System.out.println("Czas: "+task.getEventTime().toGMTString());
		else
			System.out.println("Czas: teraz");
	}
	
	
	public static String getSysTime(){
		GregorianCalendar gc = new GregorianCalendar();
		Date date =  gc.getTime();
		StringBuilder sb = new StringBuilder();
		sb.append(date.getYear() + 1900).append("-").append(date.getMonth() + 1).append("-").append(date.getDate()).append(" ");
		sb.append(date.getHours()).append(":").append(date.getMinutes()).append(":").append(date.getSeconds());
		return sb.toString();
    }
    
    public static String generateNewPassword(){
    	char [] newPass = new char[6];
    	Random r = new Random();
    	int type = 0, pos=0;
    	for(int i = 0; i < 6; i++){
    		type = r.nextInt(1);
    		if(type == 1){
    			pos = r.nextInt(23);
    			newPass[i] = alphabet[pos];
    		}
    		else{
    			pos = r.nextInt(9);
    			newPass[i] = Integer.toString(pos).charAt(0);
    		}
    	}
    	return newPass.toString();
    	//return md5sum(newPass.toString());
    }  

    public static String md5sum(String input) {
    	byte[] source;
    	try {
    		//Get byte according by specified coding.
    		source = input.getBytes("UTF-8");
    	} catch (UnsupportedEncodingException e) {
    		source = input.getBytes();
    	}
    	String result = null;
    	char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    	try {
    		MessageDigest md = MessageDigest.getInstance("MD5");
    		md.update(source);
        //The result should be one 128 integer
    		byte temp[] = md.digest();
    		char str[] = new char[16 * 2];
    		int k = 0;
    		for (int i = 0; i < 16; i++) {
    			byte byte0 = temp[i];
    			str[k++] = hexDigits[byte0 >>> 4 & 0xf];
    			str[k++] = hexDigits[byte0 & 0xf];
    		}
    		result = new String(str);
    	} catch (NoSuchAlgorithmException e) {
            System.out.println(e);
    	}
    	return result;
    }
}

